package com.qf.utils;

import org.apache.curator.RetryPolicy;
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.CuratorFrameworkFactory;
import org.apache.curator.retry.ExponentialBackoffRetry;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * @author zhaojian
 */
@Configuration
public class ZkConfig {

    /**
     * 获取zookeeper连接操作实例
     * @return
     */
    @Bean
    public CuratorFramework cf(){
        //1. 设置连接最大超时时间, 以及超时重试次数
        RetryPolicy retryPolicy = new ExponentialBackoffRetry(3000,2);
        //2. 创建集群节点连接操作对象
        CuratorFramework cf = CuratorFrameworkFactory.builder()
                .connectString("192.168.200.129:2181")
                //连接超时时间
                .connectionTimeoutMs(600000)
                //zookeeper会话超时时间
                .sessionTimeoutMs(600000)
                .retryPolicy(retryPolicy)
                .build();
        //3. 开始连接
        cf.start();
        System.out.println("zk operation");
        //4. 返回对象
        return cf;
    }
}